package com.tentact.coolmeetingtest.service;

import com.tentact.coolmeetingtest.dao.DeptDao;
import com.tentact.coolmeetingtest.dao.EmpDao;
import com.tentact.coolmeetingtest.dao.MeetingDao;
import com.tentact.coolmeetingtest.dao.RoomDao;
import com.tentact.coolmeetingtest.dto.DeptWithEmpList;
import com.tentact.coolmeetingtest.dto.MeetingWithAttendEmps;
import com.tentact.coolmeetingtest.vo.Dept;
import com.tentact.coolmeetingtest.vo.Emp;
import com.tentact.coolmeetingtest.vo.Meeting;
import com.tentact.coolmeetingtest.vo.Room;

import java.sql.SQLException;
import java.sql.SQLTransientException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 星环
 * @date 2023/2/5 20:55:18
 * @description
 */
public class MeetingService {

    DeptDao deptDao = new DeptDao();
    EmpDao empDao = new EmpDao();

    RoomDao roomDao = new RoomDao();

    MeetingDao meetingDao = new MeetingDao();
    public List<DeptWithEmpList> getAllDeptsWithEmps() {
        //deptno -> dept : list
        Map<Integer, DeptWithEmpList> deptMap = new HashMap<>();
        List<Dept> allDept = deptDao.getAllDept();
        List<Emp> allEmp = empDao.getAll();
        allDept.forEach(dept -> {
            deptMap.put(dept.getDeptno(),new DeptWithEmpList(dept,new ArrayList<>()));
        });
        allEmp.forEach(emp -> {
            deptMap.get(emp.getDeptno()).getEmpList().add(emp);
        });

        return deptMap.values().stream().toList();
    }

    public int bookMeeting(Meeting meeting) {
        if(!empDao.checkExistByEmpno(meeting.getBookerno())){
            return -1;
            //return RetMsg.msg(-1,"会议预定者不存在");
        }
        if(meeting.getExpectedPersonCount() <= 0){
            return -2;
            //return RetMsg.msg(-2, "人数应为正整数");
        }
        if(meeting.getStartTime().after(meeting.getEndTime())){
            return -3;
            //return RetMsg.msg(-3,"时间设定有误");
        }
        Room room = roomDao.selectAvalidSmallestRoom(meeting.getStartTime(), meeting.getEndTime(), meeting.getExpectedPersonCount());
        if(room == null){
            return -4;
            //return RetMsg.msg(-1,"找不到可用的会议室");
        }
        meeting.setRoomno(room.getRoomno());
        System.out.println(meeting);
        System.out.println("找到可用会议室，会议室为" + room);
        return meetingDao.addMeeting(meeting);

        //return RetMsg.msg(1,"预定成功,房间为" + room.getRoomno());
    }

    public void addMembers(int meetingno, List<Integer> empList) throws SQLTransientException {
        int index = 0;
        System.out.println(empList);
        try {
            for(;index < empList.size();index++){
                int ret = meetingDao.addMember(meetingno,empList.get(index));
                if(ret == 0){
                    throw new SQLTransientException("添加会议成员失败");
                }
            }
        }catch (SQLException e){
            for(int i = 0;i < index;i++){
                meetingDao.deleteMember(meetingno,empList.get(i));
            }
            throw e;
        }
    }

    public int deleteOnlyMeeting(Meeting meeting) {
        return meetingDao.deleteMeetingByMeetingno(meeting.getMeetingno());
    }


    public List<Meeting> getMeetingInFuture7DaysByEmpno(int userno) {
        return meetingDao.getMeetingInFuture7DaysByEmpno(userno);
    }

    public MeetingWithAttendEmps getMeetingDetailByMeetingNo(int meetingno) {
        Meeting meeting = meetingDao.getMeetingByMeetingno(meetingno);
        List<Emp> empList = meetingDao.getMembersByMeetingno(meetingno);
        return new MeetingWithAttendEmps(meeting,empList);
    }

    public List<Meeting> getActiveMeetingByBookerNo(int bookerno){
        return meetingDao.getActiveMeetingByBookerNo(bookerno);
    }

    public List<Meeting> getMeetingsByAttendEmpno(int empno) {
        String sql = """
                select m.*,e.emp_name from meeting m join emp e on m.bookerno = e.empno where meetingno in (
                    select meetingno from meeting_emp
                    where empno = ?)
                """;
        //PreparedStatement ps =
        return null;
    }

    public void cancelMeeting(int meetingno) {
        meetingDao.cancleMeeting(meetingno);
    }

    public List<Meeting> getFullDetailedMeetingsByAttemptedEmpno(int empno) {
        return meetingDao.getFullDetailedMeetingsByAttemptedEmpno(empno);
    }
}
